package com.maxuxu.scihome.controller;

import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.User;
import com.maxuxu.scihome.service.MailService;
import com.maxuxu.scihome.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @className: LoginUpController
 * @author: Maxuxu
 * @date: 2022/7/8 9:57
 * @description: 注册功能
 */
@Controller
@Slf4j
public class LoginUpController {
    @Autowired
    UserService userService;
    @Autowired
    MailService mailService;
    @Autowired
    Message message;

    @GetMapping("/logup")
    public String loginup(){
        return "adduser";
    }

    @PostMapping("/adduser")
    @ResponseBody
    public Message adduser(String loginname,String password,String email){
        log.error(loginname, password, email);
        User user = new User();
        user.setLoginname(loginname);
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5password);
        user.setStatus(2);
        user.setEmail(email);
        userService.insertUser(user);
        mailService.sendMail("870839344@qq.com",email,"账号注册成功","恭喜成功注册员工之家账号");
        message.setJson("success");
        return message;
    }

}
